package com.hrm.houfei.dao.impl;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.hrm.houfei.dao.IHrmConfigDAO;
import com.hrm.houfei.po.Hrmconfig;
import com.hrm.houfei.vo.QueryHrmConfigGroupByItemResult;

/**
 * A data access object (DAO) providing persistence and search support for
 * Hrmconfig entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.hrm.houfei.po.Hrmconfig
 * @author MyEclipse Persistence Tools
 */

public class HrmconfigDAO extends HibernateDaoSupport implements IHrmConfigDAO {
	private static final Log log = LogFactory.getLog(HrmconfigDAO.class);
	private int maxResults;

	protected void initDao() {
		// do nothing
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.hrm.houfei.dao.impl.IHrmConfigDAO#save(com.hrm.houfei.po.Hrmconfig)
	 */
	public void save(Hrmconfig transientInstance) {
		log.debug("saving Hrmconfig instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/**
	 * 多条件分页模糊查询
	 * 
	 * @param criteria
	 *            条件标准查询
	 * @param start
	 *            索引
	 * @param limit
	 *            每要显示的记录的条数
	 * @return 当前页所有信息
	 * 
	 */
	public List<Hrmconfig> findByPage(DetachedCriteria criteria, int start,
			int limit) {
		HibernateTemplate ht = super.getHibernateTemplate();
		// 执行分页查询
		List<Hrmconfig> list = ht.findByCriteria(criteria, start, limit);
		return list;
	}

	/**
	 * 按照分组查询数据字典信息
	 * 
	 * @param groupCondition
	 *            分组条件
	 * @return
	 */
	public List findAllGroupByItem(String groupCondition) {
		// 断线查询
		DetachedCriteria criteria = DetachedCriteria.forClass(Hrmconfig.class);
		// 按照cfgItem分组
		criteria.setProjection(Projections.groupProperty(groupCondition));
		HibernateTemplate ht = super.getHibernateTemplate();
		List<QueryHrmConfigGroupByItemResult> list = ht
				.findByCriteria(criteria);
		return list;
	}

	/**
	 * 根据条件获取查询总记录的条数
	 * 
	 * @param criteria
	 * @return
	 */
	public int getListCoutByCondition(DetachedCriteria criteria) {
		HibernateTemplate ht = super.getHibernateTemplate();
		// 执行查询
		int result = ht.findByCriteria(criteria).size();
		return result;
	}

	/**
	 * hql查询
	 * 
	 * @param hql
	 * @return List
	 */
	public List findByHql(String hql) {
		List list = super.getHibernateTemplate().find(hql);
		return list;
	}

	/**
	 * 更新数据字典信息
	 * 
	 * @param entity
	 */
	public void updateHrmconfig(Hrmconfig entity) {
		log.debug("updating Hrmconfig instance");
		try {
			super.getHibernateTemplate().update(entity);
		} catch (RuntimeException re) {
			log.error("update failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.hrm.houfei.dao.impl.IHrmConfigDAO#delete(com.hrm.houfei.po.Hrmconfig)
	 */
	public void delete(Hrmconfig persistentInstance) {
		log.debug("deleting Hrmconfig instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.hrm.houfei.dao.impl.IHrmConfigDAO#findById(java.lang.Integer)
	 */
	public Hrmconfig findById(java.lang.Integer id) {
		log.debug("getting Hrmconfig instance with id: " + id);
		try {
			Hrmconfig instance = (Hrmconfig) getHibernateTemplate().get(
					"com.hrm.houfei.po.Hrmconfig", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.hrm.houfei.dao.impl.IHrmConfigDAO#findByExample(com.hrm.houfei.po.Hrmconfig)
	 */
	public List findByExample(Hrmconfig instance) {
		log.debug("finding Hrmconfig instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.hrm.houfei.dao.impl.IHrmConfigDAO#findByProperty(java.lang.String,
	 *      java.lang.Object)
	 */
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Hrmconfig instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Hrmconfig as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.hrm.houfei.dao.impl.IHrmConfigDAO#findAll()
	 */
	public List findAll() {
		log.debug("finding all Hrmconfig instances");
		try {
			String queryString = "from Hrmconfig";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.hrm.houfei.dao.impl.IHrmConfigDAO#merge(com.hrm.houfei.po.Hrmconfig)
	 */
	public Hrmconfig merge(Hrmconfig detachedInstance) {
		log.debug("merging Hrmconfig instance");
		try {
			Hrmconfig result = (Hrmconfig) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.hrm.houfei.dao.impl.IHrmConfigDAO#attachDirty(com.hrm.houfei.po.Hrmconfig)
	 */
	public void attachDirty(Hrmconfig instance) {
		log.debug("attaching dirty Hrmconfig instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.hrm.houfei.dao.impl.IHrmConfigDAO#attachClean(com.hrm.houfei.po.Hrmconfig)
	 */
	public void attachClean(Hrmconfig instance) {
		log.debug("attaching clean Hrmconfig instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static IHrmConfigDAO getFromApplicationContext(ApplicationContext ctx) {
		return (IHrmConfigDAO) ctx.getBean("HrmconfigDAO");
	}
}